from time import sleep

import myserial
import main
#查询
cmd_query = [0xEF, 0x41, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x41 ,0]
#微调增加功率
cmd_add = [0xEF,0x45,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x45,0x00]
#微调减小功率
cmd_dec = [0xEF,0x46,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x46,0x00]
#切换频组
cmd_change_band = [0xEF,0x62,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x62,0x00]
#切换频点
cmd_change_pt = [0xEF,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x42,0x00]
#切换功率
cmd_change_pw = [0xEF,0x43,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x43,0x00]
#切出PIT
cmd_change_pit = [0xEF,0x43,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x44,0x00]
#保存
cmd_save = [0xEF,0x53,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x53,0x00]

def init():
    myserial.send(cmd_query)
    sleep(0.2)
    stat=myserial.receive()
    band_now = stat[2]
    pt_now = stat[3]
    power_now = stat[4]
    pit_now = stat[5]

    if  band_now != 0x01:
        for i in range(0,(main.fre_band_num+1-band_now)):
            change_band()

    if pt_now != 0x01:
        for i in range(0,(main.fre_pts_num+1-pt_now)):
            change_pt()

    if pit_now != 0x01:
        for i in range(0,(main.power_level_num+1-power_now)):
            change_pw()

def query():
    myserial.send(cmd_query)
    sleep(0.5)
    myserial.receive()

def add():
    myserial.send(cmd_add)
    sleep(0.5)

def dec():
    myserial.send(cmd_dec)
    sleep(0.5)

def change_band():
    myserial.send(cmd_change_band)
    sleep(0.3)

def change_pt():
    myserial.send(cmd_change_pt)
    sleep(0.3)

def change_pw():
    myserial.send(cmd_change_pw)
    sleep(0.5)

def change_pit():
    myserial.send(cmd_change_pit)
    sleep(0.5)

def save():
    myserial.send(cmd_save)
    sleep(0.3)
